package com.taiyuan.tongcheng.adapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.StringUtils;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.taiyuan.tongcheng.bean.MoneyCardDto;
import com.tytc56.user.R;
/**
 * 
* @Description 优惠券adapter
* @author Administrator  
* @date 2015年9月16日 下午11:09:28 
* @update (date)
* @version V1.0
 */
public class MoneyCardAdapter extends BaseAdapter {
	private LayoutInflater layoutInflater;
	private Context context;
	private List<MoneyCardDto> moneyCardList = new ArrayList<MoneyCardDto>();
	private int selectPosition = 666;

	public MoneyCardAdapter(Context context,List<MoneyCardDto> moneyCardList) {
		this.context = context;
		this.layoutInflater = LayoutInflater.from(context);
		this.moneyCardList=moneyCardList;
	}

	public int getSelecPosition() {

        return selectPosition;	
	}

	public void setSlectPosition(int postion) {
		
		 if(postion != selectPosition){
		      this.selectPosition = postion;
		     notifyDataSetChanged();
		     }
	}

	public int getCount() {
		return moneyCardList.size();
	}

	public Object getItem(int position) {
		return moneyCardList.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ZuJian zuJian = null;
		if (convertView == null) {
			zuJian = new ZuJian();
			convertView = layoutInflater.inflate(R.layout.listview_moneycard,
					null);
			zuJian.cardName=(TextView)convertView.findViewById(R.id.tv_money_card_name);
			zuJian.cardPrice=(TextView)convertView.findViewById(R.id.tv_moneycard_value);
			zuJian.cardDate=(TextView)convertView.findViewById(R.id.tv_money_date);
			zuJian.cardSelectImg=(ImageView)convertView.findViewById(R.id.iv_money_select);
			convertView.setTag(zuJian);
		} else {
			zuJian = (ZuJian) convertView.getTag();
		}
		zuJian.cardName.setText("【优惠劵】"+moneyCardList.get(position).getName());
		zuJian.cardPrice.setText("￥"+getIntPrice(moneyCardList.get(position).getMoney()));
		zuJian.cardDate.setText("有效期："+new SimpleDateFormat("yyyy-MM-dd").format(StringUtils.toDate(moneyCardList.get(position).getDeadline())));
	
		if (position == selectPosition) {
			zuJian.cardSelectImg.setVisibility(View.VISIBLE);
		} else {
			zuJian.cardSelectImg.setVisibility(View.GONE);
		}
		return convertView;
	}

	public final class ZuJian {
		public TextView cardPrice;
		public ImageView cardSelectImg;
		public TextView cardName;
		public TextView cardDate;
	}

	
	public String getIntPrice(String price){
		return  price.substring(0, price.indexOf("."));
	}
	
	
}